<?php

namespace App\Repositories\Eloquent\Mgt;

use Illuminate\Container\Container as App;
use App\Repositories\Eloquent\Repository;

class EnumRepository extends Repository
{

    public function __construct(App $app)
    {
        parent::__construct($app);
    }
    
    /**
     * 指定模型名称
     *
     * @return mixed
     */
    function model()
    {
        return 'App\Models\Mgt\Enum';
    }

    /**
     * permission列表
     *
     * @param string $query
     * @return mixed
     */
    public function index($query = '')
    {
        // 获取分页数据
        if (!$query) {
            $result = $this->model
                ->orderBy('order');
        } else {
            // 若有查询参数
            $result = $this->model
                ->where('title', 'like', "%$query%");
        }

        return $result;
    }

    /**
     * 查找指定permission
     *
     * @param $id
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function show($id)
    {
        $param = $this->find($id);        
        if ($param) {            
            return $this->responseSuccess('获取参数数据成功', $param);
        }
        
        return $this->responseFail('获取参数数据失败');
    }

    /**
     * 存储permission
     *
     * @param $request
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function store($request)
    {
        $name = $request->controller;

        if(!empty($request->action))
            $name = $name .'-'.$request->action;

        $permission = $this->create([
            'title' => $request->title,
            'parent' => $request->isparent == 1 ? 0:$request->parent,
            'controller' => $request->controller,
            'action' => $request->action,
            'name' => $name,
            'icon' => $request->icon,
            'order' => $request->order,
            'status' => STATUS_NORMAL
        ]);
        if ($result = !!$permission) {
            return $this->responseSuccess('添加权限成功', $permission, route('admin.system.permission.index'));
        }            

        return $this->responseFail('添加权限失败');
    }

    /**
     * 更新permission
     *
     * @param $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function renew($request, $id)
    {
        //新权限控制器名
        $name = $request->controller;        

        $permission = $this->find($id);   

        //原权限为一级菜单，如果更改控制器名，
        //则更新权限名称，并且子菜单权限名和控制器全部更新        
        if($permission->parent == 0){
            //控制器名称未改
            if(strcmp($name, $permission->name) == 0){
                $result = $this->update([
                    'title' => $request->title,
                    'icon' => $request->icon,
                    'order' => $request->order,
                ], $id);
            }
            //控制器名称已更改
            else{
                $result = $this->update([
                    'title' => $request->title,
                    'controller' => $name,
                    'name' => $name,
                    'icon' => $request->icon,
                    'order' => $request->order,
                ], $id);
                //子菜单
                $children = $permission->children;
                foreach($children as $child){
                    $child->controller = $name;
                    $child->name = $name .'-'.$child->action;
                    $child->update();
                }
            }            
            return $this->responseSuccess('更新权限成功', $permission);

        }
        //原权限为子菜单，则不可更改控制器名
        else{
            $name = $name .'-'.$request->action;
            //未修改模块名称
            if(strcmp($request->action, $permission->action) == 0){
                $result = $this->update([
                    'title' => $request->title,
                    'icon' => $request->icon,
                    'order' => $request->order,
                ], $id);
            }
            //修改模块名
            else{
                 $result = $this->update([
                    'title' => $request->title,
                    'parent' => $request->parent,
                    'action' => $request->action,
                    'name' => $name,
                    'icon' => $request->icon,
                    'order' => $request->order,
                ], $id);
            }
           
            return $this->responseSuccess('更新权限成功', $permission);
        }   
                     
        return $this->responseFail('更新权限失败');
    }

    /**
     * 删除permission
     *
     * @param $ids
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function destroy($ids)
    {
        return $this->respondWith(['deleted' => !!$this->delete($ids)]);
    }


    /**
     * [get description]
     * @return [type] [description]
     */
    public function gets($pid)
    {
        return $this->model
            ->where('parent', $pid)
            ->where('status', STATUS_NORMAL)
            ->orderBy('order', 'asc')
            ->get();        
    }


    public function parents()
    {
        return $this->model->where('parent', 0)->get();
    }


    /**
     * [get description]
     * @return [type] [description]
     */
    public function get()
    {
        $rows = $this->model
            ->where('status', STATUS_NORMAL)
            ->orderBy('order', 'asc')
            ->get();

        return $this->children($rows);
    }

    /**
     * [children description]
     * @param  [type]  $data [description]
     * @param  integer $pid  [description]
     * @return [type]        [description]
     */
    public function children($data, $pid = 0)    
    {
        $tree = array();
        foreach($data as $key => $value) {            
            if($value['parent'] === $pid) { 
                $value['children'] = $this->children($data, $value['id']);
                $tree[] = $value;
            }
        }
        return $tree;
    }
}